package service.Impl;

import com.alibaba.fastjson.JSONObject;
import mapper.ForwordUserDao;

import entity.ForwordUser;
import org.apache.ibatis.jdbc.Null;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import service.ForwordUserService;

import javax.annotation.Resource;
import java.util.List;

/**
 * (ForwordUser)表服务实现类
 *
 * @author makejava
 * @since 2021-12-01 10:35:32
 */
@Service("ForwordUserService")
public class ForwordUserServiceImpl implements ForwordUserService {
    @Resource
    private ForwordUserDao forwordUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ForwordUser queryById(Integer id) {
        return this.forwordUserDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<ForwordUser> queryAllByLimit(int offset, int limit) {
        return this.forwordUserDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param forwordUser 实例对象
     * @return 实例对象
     */
    @Override
    public ForwordUser insert(ForwordUser forwordUser) {
        this.forwordUserDao.insert(forwordUser);
        return forwordUser;
    }

    /**
     * 修改数据
     *
     * @param forwordUser 实例对象
     * @return 实例对象
     */
    @Override
    public ForwordUser update(ForwordUser forwordUser) {
        this.forwordUserDao.update(forwordUser);
        return this.queryById(forwordUser.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.forwordUserDao.deleteById(id) > 0;
    }

    @Override
    public JSONObject loginuser(JSONObject object) {
        JSONObject resultobj=new JSONObject();
        if(object!=null&&(!StringUtils.isEmpty(object.getString("username"))||!StringUtils.isEmpty(object.getString("password")))){
            ForwordUser loginuser = forwordUserDao.loginuser(object.getString("username"), object.getString("password"));
            if(loginuser!= null){
                resultobj.put("success",true);
                resultobj.put("name",loginuser.getName());
                resultobj.put("role",loginuser.getRole());
                resultobj.put("msg","登录成功");
            }else{
                resultobj.put("success",false);
                resultobj.put("msg","登录失败");
            }
        }else{
            resultobj.put("success",false);
            resultobj.put("msg","用户名或者密码不能为空!");
            return resultobj;
        }
        return resultobj;
    }
}